using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CHEnhanceEfCore.Annotation.auth;
using CHEnhanceEfCore.Common;
using CHEnhanceEfCore.Config.Cache;
using CHEnhanceEfCore.Config.MQ.factory;
using CHEnhanceEfCore.Models;
using CHEnhanceEfCore.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace CHEnhanceEfCore.Controller
{
    public class UserController : BaseApiController
    {
        private readonly ICacheContext _cacheContext;
        private readonly RabbitMqTemplate _rabbitMqTemplate;

        private readonly IUserService _userService;

        public UserController(ICacheContext cacheContext, RabbitMqTemplate rabbitMqTemplate, IUserService userService)
        {
            _cacheContext = cacheContext;
            _rabbitMqTemplate = rabbitMqTemplate;
            _userService = userService;
        }

        [HttpGet("sendMessage")]
        public async Task<Result> testMqSendMessage(string msg)
        {
            // User user = new User();
            // user.id=Guid.NewGuid();
            // user.account = "account";
            // user.password = "password";
            // var userJsonString = JsonConvert.SerializeObject(user);
            //可以传json对象的字符串作为消息
            await _rabbitMqTemplate.sendMessageToQueue("order", "hello");

            return Result.success();
        }

        [HttpPost("add")]
        public async Task<Result> add([FromBody] User user)
        {
            if (user == null)
            {
                return Result.fail("参数不能为空");
            }

            await _userService.saveAsync(user);

            return Result.success(user);
        }

        /// <summary>
        /// 返回用户的集合
        /// todo:后续做分页
        /// </summary>
        /// <returns></returns>
        // [Authorize(policy: "UserPolicy")]
        // [Permission("sys:user:list")]
        [HttpGet("list")]
        public async Task<Result> list()
        {
            List<User> userList = await _userService.getUserListAsync();
            return Result.success(userList);
        }

        [HttpGet("detail")]
        public async Task<Result> detail(Guid userId)
        {
            if (userId == Guid.Empty)
            {
                return Result.fail("参数不能为空");
            }

            User user = await _userService.getUserInfoAsync(userId);
            return Result.success(user);
        }
    }
}